package com.it.sun.easychat.framework.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author sunmannian
 * @ClassName Swagger2Configuration
 * @Date 2020/5/23 21:34
 * @Version V1.0
 * Description  <请输入一句话描述该类功能>
 * Copyright (C) 北京中电兴发科技有限公司  since 2020/5/23
 */
@EnableSwagger2
@Configuration
//@Profile({"dev"})
public class Swagger2Configuration extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("doc.html").
                addResourceLocations("classpath:/META-INF/resources/");
    }
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/swagger", "/swagger-ui.html").
                setStatusCode(HttpStatus.PERMANENT_REDIRECT);
        registry.addRedirectViewController("/doc", "/doc.html").
                setStatusCode(HttpStatus.PERMANENT_REDIRECT);
    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).
                apiInfo(apiInfo()).
                select().
                apis(RequestHandlerSelectors.basePackage("com.it.sun.esaychat")).
                paths(PathSelectors.any()).
                build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().
                contact(new Contact("sunmannian", "#", "masterchile@163.com")).
                license("Apache 2.0").
                licenseUrl("http://www.apache.org/licenses/LICENSE-2.0").
                version("V1.0").
                title("易聊软件后台服务").
                description("易聊软件后台服务").
                build();
    }
}
